<template>
  <div class="tank-map container">
    <div class="img-logo"></div>
    <div class="center">
      <Form
        class="map-form"
        :rules="ruleCustom"
        ref="formValidate"
        :model="formValidate"
      >
        <div class="map-edit-page" @click="goMapEditPage">
          点击前往地图编辑器， 生成地图文件后在此处上传
        </div>
        <FormItem class="input-player" label="" prop="mapFileName">
          <input type="file" @change="changeFile" />
          <div class="add">
            <div class="add-icon"></div>
            <div class="text">上传添加</div>
          </div>
          <div class="file-name">
            {{
              formValidate.mapFileName ? formValidate.mapFileName : "点击上传"
            }}
          </div>
        </FormItem>
        <Button
          class="btn map-submit"
          type="primary"
          @click.prevent.stop="mapSubmit('formValidate')"
          >提交
        </Button>
        <div class="tips">
          注：每人只能提交一次地图。如果您已提交过地图，再次提交会覆盖上次提交内容。
        </div>
      </Form>
    </div>
  </div>
</template>
<script>
import { Form, FormItem } from "view-design";
import { registerMap } from "./utils/request";
import { getSSOTokenInfo } from "@/views/Tank/utils/index.js";

export default {
  components: {
    Form,
    FormItem,
  },
  data() {
    const mapFileNameRule = (rule, value, callback) => {
      if (value.trim() === "") {
        this.$toast.center("请上传地图文件");
        return callback(new Error(""));
      }
      callback();
    };
    return {
      userInfo: {},
      formValidate: {
        teamName: "",
        mapFile: null,
        mapFileName: "",
      },
      ruleCustom: {
        mapFileName: [
          {
            required: true,
            validator: mapFileNameRule,
            trigger: "change",
            transform(value) {
              return value.trim();
            },
          },
        ],
      },
    };
  },
  methods: {
    async mapSubmit(name) {
      let that = this;
      this.$refs[name].validate(async (valid) => {
        if (valid) {
          let formData = new FormData();
          formData.append("uid", this.userInfo.email);
          formData.append("user", "");
          formData.append("map", that.formValidate.mapFile);
          try {
            const { data } = await registerMap(formData);
            if (data && data.status === 100000) {
              that.$toast("上传成功");
            } else {
              data.desc && that.$toast(data.desc);
            }
          } catch (e) {
            console.log("mapSubmit err", e);
          }
        }
      });
    },
    checkFileType(fileName) {
      // 获取文件扩展名
      var suffixStr = fileName.split(".").pop();
      if (suffixStr !== "js") {
        this.$toast.center("请上传js文件");
        return false;
      }
      return true;
    },
    changeFile(e) {
      let file = e.target.files[0];
      if (this.checkFileType(file.name)) {
        this.formValidate.mapFileName = file.name;
        this.formValidate.mapFile = file;
      }
    },
    goMapEditPage() {
      let url = `http://aitank.sohurdc.com/editor/`;
      window.open(url);
    },
  },
  mounted() {
    const userInfo = getSSOTokenInfo();
    this.userInfo = userInfo;
    console.log("userInfo", userInfo);
  },
  destroyed() {},
};
</script>
<style lang="scss" scoped>
/deep/.ivu-form-item-error-tip {
  display: none;
}
.container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  overflow-x: auto;
  background: url(./assets/tank_reg_bg@1x.jpg) no-repeat center center;
  background-size: 100% 100%;
  box-sizing: border-box;
  .img-logo {
    width: 518px;
    height: 10.3%;
    background: url(./assets/ai_normal.png) no-repeat center center;
    background-size: 100%;
    position: absolute;
    top: 7.6%;
    left: 50%;
    transform: translateX(-50%);
  }
  .center {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 1200px;
    height: 100%;
    margin: 0 auto;
    overflow-y: auto;
    position: relative;
    font-family: PingFangSC-Regular, PingFang SC;
    .map-form {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 100%;
      .map-edit-page {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 627px;
        height: 15.83%;
        background: url("./assets/tank_map_edit@2x.png") no-repeat center center;
        background-size: 100% 100%;
        font-size: 24px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #ffffff;
        cursor: pointer;
        margin-bottom: 4.2%;
      }

      .input-player {
        width: 338px;
        height: 23.05%;
        background: url(./assets/tank_reg_upload@2x.png) no-repeat center center;
        background-size: 100% 100%;
        position: relative;
        /deep/.ivu-form-item-content {
          width: 100%;
          height: 100%;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
        }
        input {
          width: 100%;
          height: 100%;
          position: absolute;
          font-size: 100px;
          right: 0;
          top: 0;
          opacity: 0;
          filter: alpha(opacity=0);
          cursor: pointer;
          z-index: 9;
        }
        .add {
          height: 35.74%;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #ffffff;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          .add-icon {
            width: 54px;
            height: 60.67%;
            opacity: 0.79;
            background: url(./assets/add_icon@2x.png) no-repeat center center;
            background-size: 100% 100%;
            margin-bottom: 14.6%;
          }
          .text {
            font-size: 16px;
            line-height: 22px;
          }
        }
        .file-name {
          width: 338px;
          position: absolute;
          bottom: -50px;
          left: 50%;
          transform: translateX(-50%);
          height: 16.86%;
          font-size: 24px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #ffffff;
          line-height: 100%;
          text-align: center;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }

      /deep/.ivu-form-item {
        margin: 0;
      }

      .btn {
        border: none;
        width: 217px;
        height: 8.24%;
        background: url(./assets/tank_reg_btn@2x.png) no-repeat center center;
        background-size: 100% 100%;
        cursor: pointer;
        font-size: 24px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #ffffff;
        line-height: 100%;
        margin-top: 10%;
      }
      /deep/.ivu-form-item-content {
        margin: 0 !important;
      }

      .tips {
        margin-top: 1.38%;
        font-size: 20px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #ffffff;
        text-align: center;
      }
    }
  }
}
</style>
